﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TI_4_Limonade_Fabriek.Database;
using TI_4_Limonade_Fabriek.Model;

namespace TI_4_Limonade_Fabriek
{
    public partial class UserOverviewScreen : Form
    {
        UserDBController udbc;
        List<User> userList;

        public UserOverviewScreen()
        {
            InitializeComponent();
            udbc = new UserDBController();
            userList = new List<User>();
            FillUserList();
        }

        private void UserOverviewScreen_Load(object sender, EventArgs e)
        {
            
        }

        private void FillUserList()
        {
            userList.Clear();
            userList = udbc.ReadUserList();

            dgvUserList.DataSource = userList;
            RenameColumnHeaders();

        }

        private void RenameColumnHeaders()
        {
            dgvUserList.Columns[0].HeaderText = "Gebruiker ID";
            dgvUserList.Columns[3].HeaderText = "Gebruiker rol";

        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            InsertUserScreen giScherm = new InsertUserScreen();
            giScherm.ShowDialog();
            if (giScherm.IsDisposed)
            {
                FillUserList();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (dgvUserList.SelectedRows.Count == 1)
            {
                DialogResult choice = MessageBox.Show("Weet u zeker dat u deze gebruiker wilt verwijderen?", "Verwijderen", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (choice == DialogResult.OK)
                {
                    int UId = GetSelectedUserId();
                    if (UId != 1)
                    {
                        udbc.DeleteUser(UId);
                        FillUserList();
                    }
                    else
                    {
                        MessageBox.Show("Deze gebruiker kan niet worden verwijderd.");
                    }
                }
            }
            else
            {
                MessageBox.Show("Graag een gebruiker selecteren");
            }
        }

        private int GetSelectedUserId()
        {
            int rowindex = dgvUserList.CurrentCell.RowIndex;
            return int.Parse(dgvUserList.Rows[rowindex].Cells[0].Value.ToString());
        }
        

    }
}
